﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;

namespace Phoenix.Xna.Framework.Input.Mice
{
    /// <summary>
    /// Manages mice connected to the system.  This manager is a singleton.
    /// </summary>
    internal sealed class MouseManager : HumanInterfaceDevice
    {
        private class NestedSingleton
        {
            internal static readonly MouseManager Singleton = new MouseManager();

            static NestedSingleton() { }
        }

        /// <summary>
        /// Gets a singleton instance of the InputService.
        /// </summary>
        public static MouseManager Instance
        {
            get { return NestedSingleton.Singleton; }
        }

        private MouseManager()
            : base()
        {
            Type = DeviceType.Mouse;
            ValidTypes.Add(typeof(MouseAxisMap));
            ValidTypes.Add(typeof(MouseScrollWheelMap));
            ValidTypes.Add(typeof(MouseButtonMap));
            MouseStateManager.Instance.StateChanged += new EventHandler(OnStateChanged);
            MouseStateManager.Instance.Update();
        }

        /// <summary>
        /// Finds a ButtonMap in the internal collection that contains the specified buttons.
        /// </summary>
        /// <param name="keys">The buttons contained in the ButtonMap to find.</param>
        /// <returns>The ButtonMap that matches the specified buttons, otherwise null.</returns>
        public MouseButtonMap FindMap(List<Buttons> buttons)
        {
            foreach (MouseButtonMap m in InputMaps)
            {
                if (m.MappedButtons == buttons)
                {
                    return m;
                }
            }

            return null;
        }

        #region IHumanInterfaceDevice Members

        /// <summary>
        /// Updates the interface device.
        /// </summary>
        /// <param name="gameTime">A snapshot of the elapsed game time.</param>
        public override void Update(GameTime gameTime)
        {
            MouseStateManager.Instance.Update();
            base.Update(gameTime);
        }

        #endregion
    }
}
